function createFolder() {
	$("#spinner").spin('modal');
	var fPath = $('#currentPath').val();
	var fName = $('#nameFolderCreate').val();
	if (fName == ""){
		removeSpinner();
		$('#createFolderForm .form-group').addClass('has-feedback has-error');
		$('#createFolderForm .form-group small').css('display','block');
		return;
	}
	jQuery.ajax({
		type : 'POST',
		url : "/folder/createFolder",
		data : {
			folderName : fName,
			folderPath : fPath
		},
		async : true,
		dataType : "json",
		success : function(dataReceived) {
			removeSpinner();
			if (dataReceived.info === "Create folder successfully"){
				window.location.reload();
			}
			else{
				$('#createFolderForm .form-group').addClass('has-feedback has-error');
				$('#createFolderForm .form-group small').css('display','block');
				$('#createFolderForm .form-group small').html(dataReceived.info);
			}
		}
	});
}
$(document).ready(function(){
	$('#createFolderForm').bootstrapValidator({
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating : 'glyphicon glyphicon-refresh'
		},
		fields : {
			nameFolderCreate : {
				validators : {
					notEmpty : {
					}
				}
			}
		}
	});
	$('#createFileForm').bootstrapValidator({
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating : 'glyphicon glyphicon-refresh'
		},
		fields : {
			nameFileCreate : {
				validators : {
					notEmpty : {
					}
				}
			}
		}
	});
});
$(function(){
	$('#createFolderModal').on('hide.bs.modal', function() {
		$('#createFolderForm').bootstrapValidator('resetForm', true);
	});
	$('.btn-deleteDocument').click(function(){
		if ($('#documentTypeSelect').val() == 0){
			$('#deleteMessage').html('Are you sure you want to delete permanently folder <i style="color: red">' + $('#documentNameSelect').val() + '</i> ?');
		}
		else{
			$('#deleteMessage').html('Are you sure you want to delete permanently file <i style="color: red">' + $('#documentNameSelect').val() + '</i> ?');
		}
		$('#confirmDeleteDocumentModal').modal('show');
	});
});
$(function(){
	$('.btn-moveDocumentToTrash').click(function(){
		var fPath = $('#currentPath').val();
		var fRealPath = $('#realCurrentPath').val();
		var fEmail = $('#emailOwner').val();
		var fName = $('#documentNameSelect').val();
		var fType = $('#documentTypeSelect').val();
		if (fName == ""){
			alert("Please select folder !");
			window.location.reload();
		}
		else{
			jQuery.ajax({
				type : 'POST',
				url : "/manage/moveDocumentToTrash",
				data : {
					documentName : fName,
					documentEmail : fEmail,
					documentPath : fPath,
					documentRealPath: fRealPath,
					documentType : fType
				},
				async : true,
				dataType : "json",
				success : function(dataReceived) {
//					alert(dataReceived.info);
					if (dataReceived.info === "Move folder to trash successfully" || dataReceived.info === "Move file to trash successfully"){
//						window.location.reload();
					}
				}
			});
		}
	});
	$('.btn-restoreDocument').click(function(){
		var fPath = $('#documentPathSelect').val();
		var fName = $('#documentNameSelect').val();
		var fType = $('#documentTypeSelect').val();
		var fEmail = $('#documentEmailSelect').val();
		if (fName == ""){
			alert("Please select folder !");
			window.location.href = "/manage/trash";
		}
		else{
			jQuery.ajax({
				type : 'POST',
				url : "/manage/restoreDocument",
				data : {
					documentName : fName,
					documentPath : fPath,
					documentType : fType,
					userEmailOwner : fEmail
				},
				async : true,
				dataType : "json",
				success : function(dataReceived) {
					if (dataReceived.info === "Restore folder from trash successfully" || dataReceived.info === "Restore file from trash successfully"){
						window.location.href = "/manage/trash";
					}
					else if (dataReceived.info === "Restore folder from trash failure because folder parent is deleted" || dataReceived.info === "Restore file from trash failure because folder contain it was deleted"){
//						if ($('#documentTypeSelect').val() == 0){
//							$('#restoreMessage').html('Restoring folder <i style="color: red">' + fName + '</i> will restore the path to this folder,<br><br><span style="color: blue">Are you sure?</span>');
//						}
//						else{
//							$('#restoreMessage').html('Restoring file <i style="color: red">' + fName + '</i> will restore the path to this file,<br><br><span style="color: blue">Are you sure?</span>');
//						}
						$('#restoreMessageAlert').html('The folder contains folder <i style="color: red">' + fName + '</i>  is in trash, <br><br>Please restore folder contains this folder');
						$('#messageRestoreDocumentModal').modal('show');
					}
					else{
						alert(dataReceived.info);
					}
				}
			});
		}
	});
});
//function restoreDocumentWithFolderParent(){
//	$("#spinner").spin('modal');
//	var fPath = $('#documentPathSelect').val();
//	var fName = $('#documentNameSelect').val();
//	var fType = $('#documentTypeSelect').val();
//	var fEmail = $('#documentEmailSelect').val();
//	if (fName == ""){
//		alert("Please select folder !");
//		window.location.href = urlRedirect;
//	}
//	else{
//		jQuery.ajax({
//			type : 'POST',
//			url : "/manage/restoreDocumentWithFolderParent",
//			data : {
//				documentName : fName,
//				documentPath : fPath,
//				documentType : fType
//			},
//			async : true,
//			dataType : "json",
//			success : function(dataReceived) {
//				removeSpinner();
//				if (dataReceived.info === "Restore folder from trash successfully"){
//					window.location.reload();
//				}
//				else{
//					alert(dataReceived.info);
//				}
//			}
//		});
//	}
//}
function deletePermanentlyDocument(){
	$("#spinner").spin('modal');
	var fPathRedirect = $('#currentPath').val();
	var fPath = $('#realCurrentPath').val();
	var fEmail = $('#emailOwner').val();
	if (fPath == null){
		fPath = $('#documentPathSelect').val();
		fEmail = $('#documentEmailSelect').val();
	}
	var fName = $('#documentNameSelect').val();
	var fType = $('#documentTypeSelect').val();
	if (fName == ""){
		alert("Please select folder !");
		window.location.reload();
	}
	else{
		jQuery.ajax({
			type : 'POST',
			url : "/manage/deletePermanentlyDocument",
			data : {
				documentName : fName,
				documentPath : fPath,
				documentType : fType,
				documentEmail : fEmail
			},
			async : true,
			dataType : "json",
			success : function(dataReceived) {
				removeSpinner();
				if (dataReceived.info === "Delete folder successfully" || dataReceived.info === "Delete file successfully"){
//					window.location.reload();
					$('#confirmDeleteDocumentModal').modal('hide');
					$('#capacitySlash').html('&nbsp;&nbsp;&nbsp;&nbsp;' + dataReceived.usedCapacity + "/" + dataReceived.avaiCapacity);
					$('#capacityPercent').css('width', dataReceived.percent + '%');
				}
				else{
					alert(dataReceived.info);
				}
			}
		});
	}
}
function shareFolderToListUser(){
	var listEmail = $('#sendLinkFolderEmail').val();
	var folderUrlShare = $('#documentUrlSelect').val();
	var message = $('#sendLinkFolderMessage').val();
	if (listEmail.trim() != ""){
		jQuery.ajax({
			type : 'POST',
			url : "/folder/shareFolderToListUser",
			data : {
				folderUrl : folderUrlShare,
				listEmails : listEmail,
				shareMessage : message
			},
			async : true,
			dataType : "json",
			success : function(dataReceived) {
				if (dataReceived.info === "Share folder successfully"){
					window.location.reload();
				}
				else{
					$('#shareFolderErrorMessage').html(dataReceived.info);
					$('#shareFolderErrorModal').modal('show');
				}
			}
		});
	}
}
function createFile(){
	$("#spinner").spin('modal');
	jQuery.ajax({
		type : 'POST',
		url : "/File/createTextFile",
		data : {
			userEmail: $('#emailOwner').val(),
			filePath : $('#realCurrentPath').val(),
			fileName : $('#nameFileCreate').val() + ".txt",
			fileContent : $('#contentFileCreate').val()
		},
		async : true,
		dataType : "json",
		success : function(dataReceived) {
			removeSpinner();
			console.log(dataReceived.info);
			$('#createFileModal').modal('hide');
		}
	});
}

function sendLinkToEmail(){
	jQuery.ajax({
		type : 'POST',
		url : "/File/sendFileLinkToEmail",
		data : {
			fileUrl : $('#linkShare').val(),
			listEmailReceive : $('#sendLinkEmail').val()
		},
		async : true,
		dataType : "json",
		success : function(dataReceived) {
			$('#shareFileModal').modal('hide');
		}
	});
}
function unshareFolder(){
	jQuery.ajax({
		type : 'POST',
		url : "/Folder/leaveUnshareFolder",
		data : {
			folderName : $('#documentNameSelect').val(),
			folderPath : $('#currentPath').val()
		},
		async : true,
		dataType : "json",
		success : function(dataReceived) {
			$('#confirmUnshareFolderModal').modal('hide');
		}
	});
}